<template>
  <div>
    <FilterTable
      ref="basetable"
      :columns="columns"
      :params="fetchParams"
      :height="200"
      :datakey="'rows'"
      :dataSource="tablelist"
      :onCellChange="changeCells"
      :onColumnsChange="columns => (this.columns = columns)"
      :onSyncTableData="changetablelist"
      :onRowSelectChange="selectFun"
    >
      <template slot="controls">
        <el-button size="small" type="primary" icon="el-icon-plus" :disabled="isdisabled" @click="addFun()">新增</el-button>
        <el-button size="small" type="primary" icon="el-icon-delete" :disabled="isdisabled" @click="addRecordHandle(3)">删除</el-button>
      </template>
    </FilterTable>
    <Drawer :visible.sync="havingVisible" title="维修备件新增" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <!-- <addMaterial :projectData="projectData" :WXCList="WXCList" @close="closeJpop" @changedata="havingFun"></addMaterial> -->
    </Drawer>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
// import addMaterial from '@/pages/repair/repairReception/evaluation/material/addMaterial';
// import editMaterial from '@/pages/repair/repairReception/evaluation/material/editMaterial.vue';
// import { getcplistList } from '@/api/repair/repairReception/customer/having';
import { getpartByestimateNOList } from '@/api/repair/repairReception/evaluation/index';
import { checkDiscount } from '@/utils/validate';
export default {
  name: 'RepairStuff',
  components: {
    // addMaterial,
    editMaterial
  },
  mixins: [dictionary],
  props: ['getdata', 'isdisabled', 'dataList', 'list', 'wxlist', 'code', 'Tlist', 'isclear', 'projectData'],
  data() {
    return {
      havingVisible: false,
      fetchParams: {},
      columns: this.createTable(),
      emitdata: {},
      propsList: [],
      tablelist: [],
      editItems: [],
      WXCList: [],
      selectList: []
    };
  },
  watch: {
    dataList(val) {
      // this.insertRecords(val);
      this.propsList = val;
    },
    list(val) {
      this.initList(val);
    },
    // wxlist(val) {
    //   this.Wxlist(val);
    // },
    Tlist(val) {
      console.log('Tlist1', val);
      if (val.length > 0) {
        this.tablelist = val;
      }
    },
    isclear(val) {
      if (val) {
        this.tablelist = [];
        this.Tlist = [];
        this.$emit('changeClear', false);
      }
    }
  },
  created() {
    if (this.Tlist.length > 0) {
      this.tablelist = this.Tlist;
    }
    if (this.isclear) {
      this.tablelist = [];
      this.$emit('changeClear', false);
    }
  },
  mounted() {
    this.BaseTable = this.$refs.basetable.$refs.pageTable;
    // this.insertRecords(this.dataList);
    // 服务活动 维修组合 回显
    this.initList(this.list);
    // this.Wxlist(this.wxlist);
    this.isadd = false;
  },
  methods: {
    createTable() {
      return [
        {
          title: '备件代码',
          dataIndex: 'PART_NO',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '备件名称',
          dataIndex: 'PART_NAME',
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        {
          title: '出库单价',
          dataIndex: 'NODE_PRICE',
          sorter: true,
          filter: true,
          initialValue: 0,
          disabled: this.isdisabled,
          editable: true,
          filterType: 'number',
          defaultEditable: true,
          precision: 2
        },
        {
          title: '备件数量',
          dataIndex: 'PART_COUNT',
          sorter: true,
          filter: true,
          disabled: this.isdisabled,
          editable: true,
          defaultEditable: true,
          filterType: 'number',
          precision: 0
        },
        {
          title: '出库金额',
          dataIndex: 'PART_AMOUNT',
          sorter: true,
          filter: true,
          filterType: 'number',
          precision: 2
        }
      ];
    },
    addFun() {
      this.havingVisible = true;
    },
    delFun() {
      this.BaseTable.EXECUTE_DELETE();
    },
    changeSelect(val) {
      console.log(val, '11111');
    },
    closeJpop(val) {
      this.havingVisible = false;
    },
    havingFun(val) {
      // console.log(val, 'value');
      // SALES_PRICE  PART_SALES_PRICE
      val = {
        ...val,
        PART_SALES_PRICE: val['CLAIM_PRICE'] ? val['CLAIM_PRICE'] : '0',
        PART_SALES_AMOUNT: val['dollce'],
        RECEIVE_AMOUNT: val['dollce'],
        PART_QUANTITY: val['MIN_PACKAGE'],
        PRICE_RATE: val['PRICE_RATE'],
        CLAIM_PRICE: val['CLAIM_PRICE1'] ? val['CLAIM_PRICE'] : 0,
        SALES_PRICE: val['CLAIM_PRICE'] ? val['CLAIM_PRICE'] : 0,
        rowKey: 'A',
        COST_PRICE: val['COST_PRICE'] ? val['COST_PRICE'] : 0
      };
      console.log(val, 'value');
      this.BaseTable.EXECUTE_INSERT(val);
      this.BaseTable.EXECUTE_RESET_HEIGHT();
      // 添加回显
    },

    initList(val) {
      console.log(val, 'yici');
      this.BaseTable.EXECUTE_INSERT(val);
    },
    changetablelist(val) {
      for (const obj of val) {
        obj.PART_AMOUNT = (obj.NODE_PRICE * obj.PART_COUNT).toFixed(2);
      }
      this.WXCList = val;
      this.$emit('syncTablelist', val);
    },
    addRecordHandle(type) {
      let arr = [];
      for (let i = 0; i < this.selectList.length; i++) {
        if (this.selectList[i].ITEM_ID) {
          arr.push(this.selectList[i].ITEM_ID);
        }
      }
      this.$emit('delList', arr);
      console.log(arr, '删除');
      this.BaseTable.EXECUTE_DELETE(this.selectList);
      this.selectList = [];
    },
    selectFun(val) {
      console.log(val, '选中le');
      this.selectList = val;
    },

    changeHandle(val) {},
    changeCells(keys, obj) {
      // 通过监听表格编辑来计算价格联动
      // let key = Object.keys(keys)[0];
      // let a = key.substring(key.indexOf('|') + 1);
      // console.log(obj.NODE_PRICE,obj.PART_COUNT,'ppppp')
      // if (a == 'NODE_PRICE' || 'PART_COUNT') {
      //   obj.PART_AMOUNT = (obj.NODE_PRICE * obj.PART_COUNT).toFixed(2); // parseFloat
      // }
    }
  }
};
</script>

<style lang="less"></style>
